﻿using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;
using FlatRedBall.Graphics;
using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using FlatRedBall.Localization;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif

using NereusGame.Screens;
using NereusGame.Entities.Ships.Enemies;
using NereusGame.Entities.Ships;
using NereusGame.Entities.HBodies;
using NereusGame.Entities.Projectiles;
using FlatRedBall.Instructions;

namespace NereusGame.HUD
{
    public class PlayerHud
    {
        public const float MEDIUM_HEALTH = 0.70f;
        public const float CRITICAL_HEALTH = 0.25f;
        private const float WIDTH = 73.65235f;
        private const float HEIGHT = 41.42136f;
        protected const string CONTENT_MANAGER = "NereusFinalContent";   // Name of the contentManager
        private const int MAX_BUTTONS = 3;  // maximum number of buttons  
        private Player player;
        private Camera camera;
        private Sprite Body;
        private Sprite Healthbar;
        private float intialHealthBarScaleX;
        private Text playerHealth;
        private Text weapon;
        private Text score;
        private static Texture2D GreenHP = FlatRedBallServices.Load<Texture2D>(@"content/Entities/HUDs/PlayerHUD/GreenDot", CONTENT_MANAGER);



        // private BitmapFont customFont;
        // constructor
        public PlayerHud(Camera mCamera, Player mPlayer)
        {

            player = mPlayer;
            camera = mCamera;
            Body = new Sprite();
            Healthbar = new Sprite();
            SpriteManager.AddSprite(Body);
            Healthbar.AttachTo(Body, false);
            SpriteManager.AddSprite(Healthbar);
            SpriteManager.AddToLayer(Body, camera.Layer);
            SpriteManager.AddToLayer(Healthbar, camera.Layer);

            Body.AttachTo(camera, false);
            Body.Texture = FlatRedBallServices.Load<Texture2D>(@"content/Entities/HUDs/PlayerHUD/Bar", CONTENT_MANAGER);
            Body.Visible = true;
            SetBody();

            Healthbar.Texture = GreenHP;
            Healthbar.ColorOperation = ColorOperation.Color;
            Healthbar.Red = 0;
            Healthbar.Green = 1f;
            Healthbar.Blue = 0f;

            InitializeText();
            SetTexts();

        }


        private void InitializeText()
        {
            playerHealth = TextManager.AddText("HP: 100");
            TextManager.AddToLayer(playerHealth, camera.Layer);
            playerHealth.AttachTo(Body, false);
            playerHealth.Visible = true;



            weapon = TextManager.AddText("Weapon: Gun");
            TextManager.AddToLayer(weapon, camera.Layer);
            weapon.AttachTo(Body, false);
            weapon.Visible = true;


           



            score = TextManager.AddText("Score: 0");
            TextManager.AddToLayer(score, camera.Layer);
            score.AttachTo(Body, false);
            score.Visible = true;


        }
        // update the paused screen
        public void Update()
        {
            UpdateHPBar();
            UpdateWeapon();
            UpdateScore();
        }



        // remove sprites
        public void Destroy()
        {
            SpriteManager.RemoveSprite(Body);
            SpriteManager.RemoveSprite(Healthbar);
            TextManager.RemoveText(playerHealth);
            TextManager.RemoveText(weapon);
            TextManager.RemoveText(score);

        }


        public void SetCamera(Camera c)
        {
            camera.Layer.Remove(Body);
            camera.Layer.Remove(Healthbar);
            camera.Layer.Remove(playerHealth);
            camera.Layer.Remove(weapon);
            camera.Layer.Remove(score);
            Body.Detach();
            camera = c;
            Body.AttachTo(camera, false);
            SpriteManager.AddToLayer(Body, camera.Layer);
            SpriteManager.AddToLayer(Healthbar, camera.Layer);
            TextManager.AddToLayer(playerHealth, camera.Layer);
            TextManager.AddToLayer(weapon, camera.Layer);
            TextManager.AddToLayer(score, camera.Layer);
            SetBody();
            SetTexts();

        }

        private void SetBody()
        {
            Body.RelativePosition = new Vector3(0, -camera.YEdge + 5f, -(camera.Z - 5));
            Body.ScaleY = camera.YEdge * 0.125f;
            Body.ScaleX = camera.XEdge * 0.9f;

            Healthbar.ScaleY = Body.ScaleY * 0.16f;
            intialHealthBarScaleX = Healthbar.ScaleX = Body.ScaleX * 0.3f;
            Healthbar.RelativePosition = new Vector3(-Body.ScaleX * 0.65f, 0, 1f);



        }

        private void SetTexts()
        {
            playerHealth.RelativePosition.X = -Body.ScaleX * 0.3f;
            playerHealth.RelativePosition.Z = 1f;

            weapon.RelativePosition.X = -Body.ScaleX * 0.04f; ;
            weapon.RelativePosition.Z = 1f;

            score.RelativePosition.X = Body.ScaleX * 0.72f;
            score.RelativePosition.Z = 1f;

        }

        public void UpdateHPBar()
        {
            if (player.CurrentHealth <= 0)
            {
                Healthbar.ScaleX = 0;
            }
            else
            {
                Healthbar.ScaleX = (player.CurrentHealth / player.MaxHealth) * intialHealthBarScaleX;
                UpdateHealthBarColor();
            }
            UpdateHP();

        }

        private void UpdateHP()
        {
            int currentHP = (int)player.CurrentHealth;
            if (currentHP < 0)
                currentHP = 0;
            playerHealth.DisplayText = "HP: " + currentHP.ToString() + " / " + player.MaxHealth.ToString();
        }

        public void UpdateWeapon()
        {
            weapon.DisplayText = "Weapon: " + player.weaponInventory[player.weaponSelector].Name;
        }

       

        public void UpdateScore()
        {
            score.DisplayText = "Score: " + player.getScore();
        }

        private void UpdateHealthBarColor()
        {
            Healthbar.Red = 2 - (2 * player.CurrentHealth / player.MaxHealth);// ((player.MaxHealth - player.CurrentHealth) / player.MaxHealth);
            Healthbar.Green = player.CurrentHealth / player.MaxHealth;//(player.CurrentHealth / player.MaxHealth);


        }

    }
}
